<?php

namespace App\Kuafu\Wechat\Service;


use App\Kuafu\Shop\Service\ShopAppletService;
use App\Kuafu\Wechat\Enum\CodeEnum;
use App\Kuafu\Wechat\Exceptions\WechatException;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Order\Enum\CodeEnum as OrderCodeEnum;
use Illuminate\Support\Facades\Log;

class WechatSendMessageService extends BaseService
{
    /**
     * 订单支付成功模板消息
     * @param $openid
     * @param $shopName
     * @param $serialNum
     * @param $orderId
     * @param $orderRealPrice
     * @param $orderCreateTime
     * @return mixed
     * @throws WechatException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function PayOrderSendMessage(
        $openid,
        $shopName,
        $serialNum,
        $orderId,
        $orderCreateTime,
        $address,
        $shopId
    ) {
        $time         = strtotime($orderCreateTime);
        $timeToDate   = date('Y年m月d日 H:i', $time);
        $serialNum    = str_pad($serialNum, OrderCodeEnum::ORDER_LEADING_ZERO, "0", STR_PAD_LEFT);
        $templateInfo = ShopAppletService::getAppletTemplateInfo($shopId);
        $data         = [
            'touser'            => $openid,
            'template_id'       => $templateInfo['order'],//prod
            'page'              => config('wechat.orderInfoPage') . "?id=$orderId&shopId=$shopId",
            'data'              => [
                'number1' => ['value' => $serialNum, 'color' => '#173177'],
                //取单号 32位以内数字字母或者符号
                'thing22' => ['value' => $shopName, 'color' => '#173177'],
                //门店名称
                'thing23' => ['value' => $address, 'color' => '#173177'],
                //取餐地址
                'date4'   => ['value' => $timeToDate, 'color' => '#173177'],
                //下单时间 24小时制时间格式（支持+年月日），支持填时间段，两个时间点之间用“~”符号连接
            ],
            'miniprogram_state' => config("wechat.MINI_PROGRAM_STATE")
        ];
        $accessToken  = WechatService::getAccessToken($shopId);
        Log::channel('wechat')->info("支付成功模板消息入参：$orderId", $data);
        $url   = CodeEnum::SUBSCRIBE_URL . $accessToken;
        $title = '支付成功发送模板消息';
        return WechatService::sendMessage($url, $data, $title);
    }

    /**
     * 叫号通知
     * @param $serialNum
     * @param $name
     * @param $address
     * @param $time
     * @param $openId
     * @param $orderId
     * @param $shopId
     * @return mixed
     * @throws WechatException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function callNumberSendMessage($serialNum, $name, $address, $time, $openId, $orderId, $shopId)
    {
        $templateInfo = ShopAppletService::getAppletTemplateInfo($shopId);
        $data         = [
            'touser'            => $openId,
            'template_id'       => $templateInfo['callNum'],
            'page'              => config('wechat.orderInfoPage') . "?id=$orderId&shopId=$shopId",
            'data'              => [
                'character_string19' => ['value' => $serialNum, 'color' => '#173177'], //取餐码
                'thing23'            => ['value' => $name, 'color' => '#173177'],//门店名称
                'thing24'            => ['value' => $address, 'color' => '#173177'],//取餐地址
                'date15'             => ['value' => $time, 'color' => '#173177'],//下单时间
            ],
            'miniprogram_state' => config("wechat.MINI_PROGRAM_STATE")
        ];
        Log::channel('wechat')->info("叫号通知入参:$orderId", $data);
        $accessToken = WechatService::getAccessToken($shopId);
        $url         = CodeEnum::SUBSCRIBE_URL . $accessToken;
        $title       = '叫号通知发送模板消息';
        return WechatService::sendMessage($url, $data, $title);
    }

    /**
     * 优惠券到账通知
     * @param string $couponName 优惠券名称
     * @param int $startAt 使用时间
     * @param int $endAt 过期时间
     * @param string $remark 备注
     * @param string $openId 用户openId
     * @param int $shopId
     * @return mixed
     * @throws WechatException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function couponArrivalNoticeSendMessage(
        string $couponName,
        int $startAt,
        int $endAt,
        string $remark,
        string $openId,
        int $shopId
    ) {
        $startAt      = date('Y年m月d日 H:i', $startAt);
        $endAt        = date('Y年m月d日 H:i', $endAt);
        $templateInfo = ShopAppletService::getAppletTemplateInfo($shopId);
        $data         = [
            'touser'            => $openId,
            'template_id'       => $templateInfo['arrival'],
            'page'              => config('wechat.COUPON_LIST_PAGE') . '?shopId=' . $shopId,
            'data'              => [
                'thing1' => ['value' => $couponName, 'color' => '#173177'], //优惠券名称
                'time7'  => ['value' => $startAt, 'color' => '#173177'],//开始使用时间
                'time8'  => ['value' => $endAt, 'color' => '#173177'],//优惠券过期时间
                'thing4' => ['value' => $remark, 'color' => '#173177'],//备注
            ],
            'miniprogram_state' => config("wechat.MINI_PROGRAM_STATE")
        ];
        Log::channel('wechat')->info("优惠券到账提醒:$openId", $data);
        $accessToken = WechatService::getAccessToken($shopId);
        $url         = CodeEnum::SUBSCRIBE_URL . $accessToken;
        $title       = '优惠券到账通知发送模板消息';
        return WechatService::sendMessage($url, $data, $title);
    }

    /**
     * 优惠券过期提醒
     * @param string $notice
     * @param int $endAt
     * @param string $openId
     * @param int $shopId
     * @return mixed
     * @throws WechatException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function couponExpireNoticeSendMessage(
        string $notice,
        int $endAt,
        string $openId,
        int $shopId
    ) {
        $endAt        = date('Y年m月d日 H:i', $endAt);
        $templateInfo = ShopAppletService::getAppletTemplateInfo($shopId);
        $data         = [
            'touser'            => $openId,
            'template_id'       => $templateInfo['expire'],
            'page'              => config('wechat.COUPON_LIST_PAGE') . '?shopId=' . $shopId,
            'data'              => [
                'thing6' => ['value' => $notice, 'color' => '#173177'], //温馨提示
                'time4'  => ['value' => $endAt, 'color' => '#173177'],//优惠券过期时间
            ],
            'miniprogram_state' => config("wechat.MINI_PROGRAM_STATE")
        ];
        Log::channel('wechat')->info("优惠券过期提醒:$openId", $data);
        $accessToken = WechatService::getAccessToken($shopId);
        $url         = CodeEnum::SUBSCRIBE_URL . $accessToken;
        $title       = '优惠券过期提醒发送模板消息';
        return WechatService::sendMessage($url, $data, $title);
    }
}
